/*
* ClientGUI.java
*
* Created on May 3, 2007, 10:41 AM
*
*
* Copyright (c) 2008 Golden T Studios.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package test.chat;
// JFC
import java.io.IOException;
import com.golden.gamedev.engine.network.NetworkPacket;
import com.golden.gamedev.engine.network.packet.NetworkMessage;
import com.golden.gamedev.engine.network.tcp.TCPClient;
/**
*
* @author Paulus Tuerah
*/
public class ClientGUI extends javax.swing.JFrame implements Runnable {
/**
*
*/
private static final long serialVersionUID = -716240747423437011L;
private TCPClient client;
private String nick;
/** ************************************************************************* */
/** ***************************** CONSTRUCTOR ******************************* */
/** ************************************************************************* */
/** Creates new form ClientGUI */
public ClientGUI(TCPClient client, String nick) {
this.initComponents();
this.client = client;
this.nick = nick;
new Thread(this).start();
this.setSize(500, 400);
this.setLocationRelativeTo(null);
this.txtMessage.requestFocus();
}
public void run() {
try {
while (true) {
this.client.update(100);
NetworkPacket[] receivedPackets = this.client
.getReceivedPackets();
for (int i = 0; i < receivedPackets.length; i++) {
NetworkMessage packet = (NetworkMessage) receivedPackets[i];
this.appendText(packet.getMessage() + "\n");
}
try {
Thread.sleep(100);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
catch (IOException ex) {
ex.printStackTrace();
System.exit(-1);
}
}
private void appendText(String text) {
this.txtChat.setText(this.txtChat.getText() + text);
this.txtChat.setCaretPosition(this.txtChat.getDocument().getLength());
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code
// ">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.JLabel jLabel1;
javax.swing.JPanel jPanel1;
javax.swing.JPanel jPanel2;
jPanel2 = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
this.txtMessage = new javax.swing.JTextField();
this.btnSend = new javax.swing.JButton();
this.scrollText = new javax.swing.JScrollPane();
this.txtChat = new javax.swing.JEditorPane();
FormListener formListener = new FormListener();
this
.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setTitle("Chat Room");
jPanel2.setLayout(new java.awt.BorderLayout(5, 5));
jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10,
10, 10));
jPanel1.setLayout(new java.awt.BorderLayout(5, 0));
jLabel1.setText("Message :");
jPanel1.add(jLabel1, java.awt.BorderLayout.WEST);
this.txtMessage.addActionListener(formListener);
jPanel1.add(this.txtMessage, java.awt.BorderLayout.CENTER);
this.btnSend.setText("Send");
this.btnSend.addActionListener(formListener);
jPanel1.add(this.btnSend, java.awt.BorderLayout.EAST);
jPanel2.add(jPanel1, java.awt.BorderLayout.SOUTH);
this.txtChat.setEditable(false);
this.scrollText.setViewportView(this.txtChat);
jPanel2.add(this.scrollText, java.awt.BorderLayout.CENTER);
this.getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
this.pack();
}
// Code for dispatching events from components to event handlers.
private class FormListener implements java.awt.event.ActionListener {
FormListener() {
}
public void actionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == ClientGUI.this.txtMessage) {
ClientGUI.this.btnSendActionPerformed(evt);
}
else if (evt.getSource() == ClientGUI.this.btnSend) {
ClientGUI.this.btnSendActionPerformed(evt);
}
}
}// </editor-fold>//GEN-END:initComponents
private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnSendActionPerformed
String message = this.txtMessage.getText().trim();
if (message.length() == 0) {
return;
}
try {
this.client.sendPacket(new NetworkMessage(message));
message = this.nick + ": " + message;
this.txtMessage.selectAll();
this.appendText(message + "\n");
}
catch (IOException ex) {
ex.printStackTrace();
}
}// GEN-LAST:event_btnSendActionPerformed
// public static void main(String args[]) {
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
// new ClientGUI().setVisible(true);
// }
// } );
// }
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnSend;
private javax.swing.JScrollPane scrollText;
private javax.swing.JEditorPane txtChat;
private javax.swing.JTextField txtMessage;
// End of variables declaration//GEN-END:variables
}